package org.third.spring.schedule;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

import org.hibernate.annotations.GenericGenerator;
import org.third.common.utils.StringUtils;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * Base class for any object class that will be stored in a data store.
 */
@MappedSuperclass
@XmlRootElement
@XmlType(propOrder={"id"})
@XmlAccessorType(XmlAccessType.NONE)
public class SimplePersistedObject {
    /** Id column identifier */
    public static final String UNIQUE_ID_COLUMN_NAME = "UUID";
    //Currently PostgreSQL dosn't support the nstring. so it will use string currently. 
    // database schema validate
    //   1) validate: work 
    // Database CRUD
   //   it works for SQLServer/oracle/MySQl/Postgres     
    public static final String COLUMN_TYPE_NSTRING = "string";
    public static final String COLUMN_TYPE_TEXT = "text";

    /** The unique identifier */
    private String id;

    /**
     * Creates a new instance of SimplePersistedObject
     */
    public SimplePersistedObject() {
    }

    /**
     * Creates a new instance of SimplePersistedObject as a copy of an existing SimplePersistedObject
     *
     * @param rhs the SimplePersistedObject to copy
     */
    public SimplePersistedObject(SimplePersistedObject rhs) {
        this.id = rhs.id;
    }

    /**
     * Gets the unique identifier of this object
     *
     * @return the unique identifier of this object, or {@code null} if it has not yet been persisted to a data store
     */
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name = UNIQUE_ID_COLUMN_NAME, unique = true, nullable = false)
    @Id
    @XmlElement(name = "id")
    public String getId() {
        return this.id;
    }

    /**
     * Sets the unique identifier of this object
     *
     * @param value the unique identifier
     */
    public void setId(String value) {
        this.id = value;
    }

    /**
     * Determines whether the unique identifier has been set
     *
     * @return {@code true} if this object has a valid unique identifier; {@code false} otherwise.
     */
    public boolean hasId() {
        return StringUtils.hasText(this.getId());
    }

    /**
     * Gets the unique identifier, ensuring that the result can be used as input to generate a hash
     *
     * @return the unique identifier, or a single space if the unique identifier has not been set
     */
    @Transient
    @JsonIgnore
    @XmlTransient
    protected String getHashableId() {
        return (this.id != null ? this.id : " ");
    }

    /**
     * Performs object-specific actions when saving
     */
    protected void onSave() {
    }

    /**
     * Initializes members through Hibernate to avoid LazyEvaluationException errors.
     */
    public void initialize() {
    }
}
